package pool

import (
	"fmt"
	"sync"
	"time"
)

type Pool struct {
	Workers  []Worker
	TaskChan chan func()
	wg       sync.WaitGroup
}

type Worker struct {
	ID int
}

func NewPool(taskNums int, workersNum int) *Pool {
	pool := &Pool{
		TaskChan: make(chan func(), taskNums),
	}

	pool.Workers = make([]Worker, workersNum)

	for i := 0; i < workersNum; i++ {
		pool.Workers[i] = Worker{ID: i}
	}

	return pool

}

func (p *Pool) Submit(task func(), id int) {
	p.wg.Add(1)
	time.Sleep(1 * time.Second)
	fmt.Println("Submitting task")
	p.TaskChan <- task
}

func (p *Pool) Shutdown() {
	close(p.TaskChan)
	p.wg.Wait()
}

func (w *Worker) Start(p *Pool) {
	go func() {
		for task := range p.TaskChan {
			task()
			time.Sleep(1 * time.Second)
			fmt.Println("Task Finished")
			p.wg.Done()
		}
	}()
}
